--[[
@classmod Cursor
]]

local lume = require (LIB_PATH .. "lume")

return pl.class {
_name = "Cursor", 
_init = function (self, s, grid)
	self.x, self.y = 0, 0
	self.rx, self.ry = 0, 0
	self.dx = 0
	self.dy = 0
	self.w = s
	self.h = s
	self.show = true
	self.color = {.1, .1, .1}
	self.grid = grid
	self.mode = "focused"
end,

moved = function (self)
	return lume.all({self.dx, self.old_dx, self.dy, self.old_dy},
		function (v) return v == 0 end)
end,

update = function (self)
	if Sled.state == "splash" then return end
	local rx, ry = _lm.getPosition()
	self.rx, self.ry = rx, ry
	local mx, my = Sled.current_screen:get_mouse_position ()
	mx, my = Sled.grid:snap(mx, my)
	self.x, self.y = mx, my
	self.ox, self.oy = self.ox or rx, self.oy or ry
	self.dx, self.dy = self.ox- rx, self.oy - ry
	self.x, self.y = mx, my
	local bx, by, bw, bh
	if Sled.state == "map" then
		self.w, self.h = Sled.tile_size, Sled.tile_size
		Sled.grid.sz = Sled.tile_size
		bx, by = 0, 0
		bw, bh = Sled.grid.w, Sled.grid.h
	elseif Sled.state == "focused" then
		self.w, self.h = 1, 1
		Sled.grid.sz = 1
		bx, by = Sled.grid.tx, Sled.grid.ty
		bw, bh = 8, 8
	end
	if self.x >= bx and self.y >=by and self.x < bw+bx and self.y < bh+by then
		self.in_canvas = true
	else
		self.in_canvas = false
	end
	self.ox, self.oy = rx, ry
	self.old_dx, self.old_dy = self:get_delta()
end,

draw = function (self)
	_lg.setColor(1, 1, 1)
	if self.show then
	if self.in_canvas then
		_lg.setColor(.1, .8, .5 ,1)
	else
		_lg.setColor(.1,.1,.1, .1)
	end
	_lg.setLineWidth(.1)
	_lg.rectangle("line", self.x, self.y, self.w, self.h)
	end
end,

get_position = function (self)
	return _lm.getPosition()
end,

get_delta = function (self)
	return self.dx, self.dy
end
}
